unit uSplashForm;

interface

uses
  SysUtils,
  Classes,
  Controls,
  Messages,
  Windows,
  Forms,
  pngimage,
  Graphics;

type
  TSplashEkran = class(TCustomControl)
  private
    FAlphaDegeri: integer;
    FResim: TPngImage;
    FBitmap: TBitmap;  // png resmin tutamac? i?in gerekli.
    Fhandle: THandle; // Sahip formun tutamac?.
    procedure SetAlphaDegeri(const Value: integer);
    procedure SetResim(const Value: TPngImage);
    procedure ResizCiz;
    procedure Setbuffer(const Value: TPngImage);

  protected
    procedure Paint; override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
    procedure CreateWnd; override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;

  public
    Fbuffer: TBitmap;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Execute;

  published
    property Resim: TPngImage read FResim write SetResim;
    property AlphaDegeri: integer read FAlphaDegeri write SetAlphaDegeri default 255; //
  end;

procedure register;

implementation

procedure register;
begin
  RegisterComponents('Adiitional', [TSplashEkran]);
end;
{ TSplashEkran }

constructor TSplashEkran.Create(AOwner: TComponent);
begin
  if not(AOwner is TForm) then
    raise EInvalidCast.Create('SplashForm');
  inherited Create(AOwner);
  // Controlstyle ?zelli?ine di?er bile?enleri kabul etmek i?in.
  ControlStyle := ControlStyle + [csAcceptsControls];
  // Sahip formun tutamac?n? al?yor.
  Fhandle := TForm(AOwner).Handle;
  FResim := TPngImage.Create;
  TForm(AOwner).BorderStyle := bsNone;
  FAlphaDegeri := 255;
  FBitmap := TBitmap.Create;
  Fbuffer := TBitmap.Create;
  Align := alClient;
end;

procedure TSplashEkran.CreateWnd;
begin
  // pen?ere olu?turulduktan sonra resmi buffera at?yor.
  inherited CreateWnd;
  if not(csDesigning in ComponentState) then
    Fbuffer.Assign(FResim);
end;

destructor TSplashEkran.Destroy;
begin
  FBitmap.Free;
  FResim.Free;
  Fbuffer.Free;
  inherited;
end;

procedure TSplashEkran.Execute;
var
  BlendFunction: TBlendFunction;
  BitmapPos: TPoint;
  BitmapSize: TSize;
  exStyle: DWORD;
begin
  if Fhandle = 0 then
    exit;
  if csDesigning in ComponentState then
    exit;
  if FResim = nil then
    exit;
  exStyle := GetWindowLongA(Fhandle, GWL_EXSTYLE);
  if (exStyle and WS_EX_LAYERED = 0) then
    SetWindowLong(Fhandle, GWL_EXSTYLE, exStyle or WS_EX_LAYERED);
  FBitmap.Assign(FResim);
  TForm(Owner).ClientWidth := FBitmap.Width;
  TForm(Owner).ClientHeight := FBitmap.Height;
  BitmapPos := Point(0, 0);
  BitmapSize.cx := FBitmap.Width;
  BitmapSize.cy := FBitmap.Height;
  BlendFunction.BlendOp := AC_SRC_OVER;
  BlendFunction.BlendFlags := 0;
  BlendFunction.SourceConstantAlpha := FAlphaDegeri;
  BlendFunction.AlphaFormat := AC_SRC_ALPHA;
  UpdateLayeredWindow(Fhandle, 0, nil, @BitmapSize, FBitmap.Canvas.Handle, @BitmapPos, 0,
    @BlendFunction, ULW_ALPHA);
end;

procedure TSplashEkran.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  Windows.SetFocus(Handle);
  ReleaseCapture;
  SendMessage(Fhandle, WM_SYSCOMMAND, SC_MOVE + 1, 0);
end;

procedure TSplashEkran.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  SetCapture(Handle);
end;

procedure TSplashEkran.Paint;
begin
  inherited;
  ResizCiz;
  if not(csDesigning in ComponentState) then
    Execute;
end;

procedure TSplashEkran.ResizCiz;
begin
  // Tasar?m zaman?nda atanan resmi g?stermek i?in.
  if (not FResim.Empty) then
  begin
    Canvas.Rectangle(0, 0, FResim.Width, FResim.Height);
    Canvas.Draw(0, 0, FResim);
  end;
end;

procedure TSplashEkran.SetAlphaDegeri(const Value: integer);
begin
  if FAlphaDegeri <> Value then
  begin
    FAlphaDegeri := Value;
    if FAlphaDegeri > 255 then
      FAlphaDegeri := 255;
    if FAlphaDegeri < 0 then
      FAlphaDegeri := 0;
    Invalidate;
  end;
end;

procedure TSplashEkran.Setbuffer(const Value: TPngImage);
begin
  Fbuffer.Assign(Value);
end;

procedure TSplashEkran.SetResim(const Value: TPngImage);
var
  r: TRect;
begin
  FResim.Assign(Value);
  FBitmap.Assign(FResim);
  GetWindowRect(Fhandle, r);
  MoveWindow(Fhandle, r.Left, r.Top, FResim.Width, FResim.Height, true);
  Invalidate;
end;

end.

